<template>
  <div class="m-user">
    <template v-if="user">
      欢迎您，<span class="username">{{ user }}</span> [<span
        class="exit"
        @click="exit"
        >退出</span
      >]
    </template>
    <template v-else>
      <nuxt-link to="/login" class="login">立即登录</nuxt-link>
      <nuxt-link to="/register" class="register">注册</nuxt-link>
    </template>
  </div>
</template>

<script>
export default {
  data() {
    return {
      user: ""
    };
  },
  async mounted() {
    let that = this;
    const {
      status,
      data: { user }
    } = await that.$axios.get("/users/getUser");
    if (status === 200) {
      that.user = user;
    }
  },
  methods: {
    exit() {
      this.$axios.get("/users/exit").then(res => {
        if (res.data.success) {
          window.location.reload();
        }
      });
    }
  }
};
</script>

<style>
.exit {
  cursor: pointer;
  color: #31bbac;
}
</style>
